package com.ruoyi.web.controller.ums;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.ums.entity.MemberCoupon;
import com.ruoyi.ums.service.IMemberCouponService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;
import java.util.List;

/**
 * 用户优惠券Controller
 *
 * @author ruoyi
 * @date 2021-11-04
 */
@RestController
@RequestMapping("/ums/coupon")
public class UmsMemberCouponController extends BaseController {
    @Autowired
    private IMemberCouponService umsMemberCouponService;

    /**
     * 查询用户优惠券列表
     */
    @PreAuthorize("@ss.hasPermi('ums:coupon:list')")
    @GetMapping("/list")
    public TableDataInfo list(MemberCoupon umsMemberCoupon) {
        startPage();
        List<MemberCoupon> list = umsMemberCouponService.list(new QueryWrapper<>(umsMemberCoupon));
        return getDataTable(list);
    }

    /**
     * 导出用户优惠券列表
     */
    @PreAuthorize("@ss.hasPermi('ums:coupon:export')")
    @Log(title = "用户优惠券", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(MemberCoupon umsMemberCoupon) {
        List<MemberCoupon> list = umsMemberCouponService.list(new QueryWrapper<>(umsMemberCoupon));
        ExcelUtil<MemberCoupon> util = new ExcelUtil<MemberCoupon>(MemberCoupon.class);
        return util.exportExcel(list, "用户优惠券数据");
    }

    /**
     * 获取用户优惠券详细信息
     */
    @PreAuthorize("@ss.hasPermi('ums:coupon:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(umsMemberCouponService.getById(id));
    }

    /**
     * 新增用户优惠券
     */
    @PreAuthorize("@ss.hasPermi('ums:coupon:add')")
    @Log(title = "用户优惠券", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody MemberCoupon umsMemberCoupon) {
        return toAjax(umsMemberCouponService.save(umsMemberCoupon));
    }

    /**
     * 修改用户优惠券
     */
    @PreAuthorize("@ss.hasPermi('ums:coupon:edit')")
    @Log(title = "用户优惠券", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody MemberCoupon umsMemberCoupon) {
        return toAjax(umsMemberCouponService.updateById(umsMemberCoupon));
    }

    /**
     * 删除用户优惠券
     */
    @PreAuthorize("@ss.hasPermi('ums:coupon:remove')")
    @Log(title = "用户优惠券", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        List<Long> list = Arrays.asList(ids);
        return toAjax(umsMemberCouponService.removeByIds(list));
    }
}